package artibella.core 
{
	import artibella.core.data.Config;
	import artibella.core.data.Parameters;
	import artibella.core.events.NavEvent;
	import artibella.core.INavObserver;
	import artibella.core.Nav;
	import artibella.core.rubriques.manager.Feeder;
	import artibella.IStageObserver;
	import artibella.StageManager;
	import artibella.ui.ContextMenuFeeder;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.system.Security;
	
	/**
	 * ...
	 * @author pbordachar
	 */
	
	public class AShell extends Sprite implements IStageObserver
	{
		protected static var _instance:AShell;
		
		protected var _tabNavObservers:Vector.<INavObserver>
		
		protected var _contextMenu:ContextMenuFeeder;
		protected var _rubFeeder:Feeder;
		protected var _rubHost:Sprite;
		
		public function AShell() 
		{
			Security.allowDomain( "*" );
			Security.allowInsecureDomain( "*" );
			
			if (stage) _init();
			else addEventListener(Event.ADDED_TO_STAGE, _init);
		}
		
		public static function get instance():AShell 
		{
			return _instance;
		}
		
		// - - -
		
		protected function _init( e:Event = null ):void
		{
			StageManager.instance.setStage( this.stage );
			StageManager.instance.addObserver( this );
			StageManager.instance.addResizeListener();
			
			_instance = this;
			
			Parameters.instance.setVariables( this.stage.loaderInfo );

			_buildContextMenu();
			
			_initConfig();
			
		}
		
		protected function _buildContextMenu():void
		{
			// override this
			
			_contextMenu = new ContextMenuFeeder( this, false );
			_contextMenu.addItem( "::::: Powered by Artibella :::::", null );
		}

		protected function _initConfig():void
		{
			var config:String = Parameters.instance.getValue( "config" );
			
			if ( config != "config" ) 
			{
				Config.instance.addEventListener( Event.COMPLETE, _onConfigComplete );
				Config.instance.load( config );
				
			} else {
				
				trace( " /!\ missing flashvar : config" );
			}
		}
		
		private function _onConfigComplete( e:Event ):void
		{
			Config.instance.removeEventListener( Event.COMPLETE, _onConfigComplete );
			
			// TODO : XMLList + condition
			var file:String = Config.getString( "file" );
			Config.instance.addEventListener( Event.COMPLETE, _onContentFileAdded );
			Config.instance.load( Parameters.instance.resolve( file ) );
			
			
		}
		
		private function _onContentFileAdded( e:Event ):void
		{
			Config.instance.removeEventListener( Event.COMPLETE, _onContentFileAdded );
			
			_buildContent();
			
			_initNavSystem();
		}
		
		protected function _buildContent():void
		{
			// override this
			
			_rubHost = new Sprite();
			addChild( _rubHost );
		}
		
		protected function _initNavSystem():void
		{
			if ( Config.instance.useSiteMap == true ) 
			{	
				_rubFeeder = new Feeder( _rubHost );
				
				_tabNavObservers = new Vector.<INavObserver>();
				_tabNavObservers.push( _rubFeeder );
				
				Nav.instance.addEventListener( NavEvent.CHANGE, _change );
				Nav.instance.start();
				
			} else {
				
				trace( "no sitemap tree defined in config" );
			}
		}
		
		protected function _change( e:NavEvent ):void
		{
			//trace( e );

			if( _tabNavObservers ) {
				for( var i:int = 0, nb:int = _tabNavObservers.length; i < nb; i ++ ){
					INavObserver( _tabNavObservers[ i ] ).navUpdate( e.go );
				}
			}
		}

		//-----  IStageObserver
		
		public function stageUpdate( w:int, h:int ):void 
		{
			// override this
		}
	}
}